﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Insurer {

    public class ProductModel : Configuration.ConfigurationItemModel {
                
        #region Private Properties

        private Core.Insurer.Product product = null;

        #endregion


        #region Public Properties

        public override Int64 ConfigurationItemId { set { ProductId = value; } }

        public override Core.CoreConfigurationObject ConfigurationItem { get { return product; } }


        public Int64 ProductId {

            set {

                product = Application.Core.Insurer.Product (value);

                if (product == null) {

                    product = new Core.Insurer.Product (Application);

                }

            }

        }

        public Core.Insurer.Product Product { get { return product; } set { product = value; } }


        public System.Web.Mvc.SelectList ProgramSelectList {

            get {

                List<Core.Insurer.Program> programs;

                programs = (from currentProgram in Application.Core.Insurer.Programs (false)

                            where ((currentProgram.Enabled) || (currentProgram.Id == Product.ProgramId)) // ACTIVE OR IN USE 

                            select currentProgram).ToList ();

                return new System.Web.Mvc.SelectList (programs, "Id", "Name");

            }

        }

        #endregion 


        #region Constructors

        public ProductModel (Int64 forProductId) {

            ProductId = forProductId;

            return;

        }

        public ProductModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form); // BASE RELOADS THE ORIGINAL OBJECT TYPE


            // ALLOW UPDATING OF READ-ONLY PROPERTIES FOR NEW PROGRAMS

            if (Product.Id == 0) {

                Product.ProgramId = Data.Readers.FormReader.Int64FromForm (form, "Product.ProgramId");

            }

            else {

                // RESET READ-ONLY PROPERTIES FOR EXISTING PROGRAMS 

                Product.Name = Application.Core.Insurer.Product (Product.Id, false).Name;

            }


            // EDITABLE PROPERTIES FOR NEW AND EXISTING 

            Product.UniqueId = Data.Readers.FormReader.StringFromForm (form, "Product.UniqueId");

            Product.LegacyId = Data.Readers.FormReader.StringFromForm (form, "Product.LegacyId");


            return;

        }

        #endregion 

    }

}